﻿if (typeof jQuery === "undefined") {
    throw new Error("Main requires jQuery");
}
if (typeof whoever === "undefined") {
    throw new Error("Main requires whoever");
}
if (typeof ui === "undefined") {
    throw new Error("Main requires ui");
}
if (typeof ko === "undefined") {
    throw new Error("Main requires ko");
}

var Main = (function ($, _, whoever, ui, ko, Sammy) {
    "use strict";

    var _state = (function () {

        var _hashes = [];
        _hashes.NotFound = "NotFound";

        return {
            CurrentPage: ko.observable(),
            CurrentController: ko.observable(),
            FirstLoad: ko.observable(true),
            PageNotFound: ko.observable(),
            IsCRUD: ko.observable(false),
            TryIndexing: ko.observable(false),
            IndexedId: ko.observable(),
            Hashes: _hashes
        };
    })();

    var _spa = (function () {
        //Refactor this shit
        Sammy(function () {
            this.get('#:hash/:id', function () {
                toastr.clear();
                var hash = this.params.hash;
                var id = this.params.id;

                var indexedHash = hash + "/" + id;

                if (Main.State.Hashes.indexOf(hash) === -1)
                    hash = Main.State.Hashes.NotFound;

                if (!Main.State.FirstLoad()) {
                    $.when(ui.Animation.MainExit()).done(function () {
                        ui.Animation.MainEnter();
                        Main.Page.ChangeHash(indexedHash);
                        Main.State.CurrentPage(hash);
                    });
                }
                else {
                    Main.State.FirstLoad(false);
                    ui.Animation.MainInit();
                    ui.Animation.MainEnter();
                    Main.Page.ChangeHash(indexedHash);
                    Main.State.CurrentPage(hash);
                }

                whoever.Form.EnableAll();

                Main.State.TryIndexing(true);
                Main.State.IndexedId(id);
            });
            this.get('#:hash', function () {
                toastr.clear();
                var hash = this.params.hash;

                if (Main.State.Hashes.indexOf(hash) === -1)
                    hash = Main.State.Hashes.NotFound;

                if (!Main.State.FirstLoad()) {
                    $.when(ui.Animation.MainExit()).done(function () {
                        ui.Animation.MainEnter();
                        Main.Page.ChangeHash(hash);
                        Main.State.CurrentPage(hash);
                    });
                }
                else {
                    Main.State.FirstLoad(false);
                    ui.Animation.MainInit();
                    ui.Animation.MainEnter();
                    Main.Page.ChangeHash(hash);
                    Main.State.CurrentPage(hash);
                }

                whoever.Form.EnableAll();

                Main.State.TryIndexing(false);
            });
            this.get('', function () {
                toastr.clear();
                this.app.runRoute('get', '#' + Main.State.CurrentPage());
            });
        });

        return {

        };
    })();

    return {
        State: _state,
        Page: _page,
        CRUD: _CRUD,
        Validation: _validation,
        Component: _component,
        SPA: _spa
    };
})(jQuery, _, whoever, ui, ko, Sammy);